package com.jing.system.utils;

import java.util.Iterator;
import java.util.Map.Entry;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.DataCons;
import com.jing.system.comm.contants.StringCons;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.system.comm.enums.RuleType;
import com.jing.weibo.pojo.system.Grade;
import com.jing.weibo.pojo.system.Rule;
import com.jing.weibo.pojo.user.UserCurrencyLog;
import com.jing.weibo.pojo.user.UserGradeLog;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.pojo.user.UserPointLog;
import com.jing.weibo.service.system.StationletterService;
import com.jing.weibo.service.user.UserCurrencyLogService;
import com.jing.weibo.service.user.UserGradeLogService;
import com.jing.weibo.service.user.UserInfoService;
import com.jing.weibo.service.user.UserPointLogService;

/**
 * 积分妞币 Operate Class
 * @author jing.yue
 * @version 1.0
 * @since 2012-09-29
 *
 */
public class PointCurrencyUtil {

	/** 用户名 */
	private String username;
	/** 类型 */
	private RuleType ruleType;
	/** 购买位置花费的妞币数 */
	private Integer currency;

	/**
	 * 设置消耗的妞币信息
	 * @param currency
	 */
	public void setCurrency(Integer currency) {
		this.currency = currency;
	}

	/**
	 * 构造函数
	 * @param ruleType	类型
	 * @param username	用户名
	 */
	public PointCurrencyUtil(RuleType ruleType, String username) {
		this.ruleType = ruleType;
		this.username = username;
	}

	/**
	 * 构造函数 [加入了购买位置花费的妞币数的参数]
	 * @param ruleType	类型
	 * @param username	用户名
	 * @param currency	购买位置花费的妞币数
	 */
	public PointCurrencyUtil(RuleType ruleType, String username, Integer currency) {
		this.ruleType = ruleType;
		this.username = username;
		this.currency = currency;
	}

	/**
	 * 处理积分和妞币的信息
	 * @throws Exception
	 */
	public void handlePointCurrency() throws Exception {
		UserInfoService userInfoService = (UserInfoService) SpringUtil.getBean("userInfoService");
		UserPointLogService userPointLogService = (UserPointLogService) SpringUtil.getBean("userPointLogService");
		UserGradeLogService userGradeLogService = (UserGradeLogService) SpringUtil.getBean("userGradeLogService");
		UserCurrencyLogService userCurrencyLogService = (UserCurrencyLogService) SpringUtil.getBean("userCurrencyLogService");

		//根据code获取规则对象
		Rule rule = DataCons.ruleMap.get(this.ruleType.getCode());
		int currencyNum = 0;
		int pointNum = 0;
		switch (this.ruleType) {
		case TYPE_EACH_UPGRADE:
			//每次升级
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_FEEDBACK_IDEA:
			//反馈一个有建议性的意见，并且审核通过
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_FIRST_APPLY_SYSTEM:
			//第一次使用该系统
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_EVERY_SIGN_IN:
			//每天签到
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_INVITE_USERS:
			//根据指定地址邀请一个用户
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_SHARE_WEIBO:
			//点击“分享它”
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_PURCHASE_SEAT:
			//购买一个位置  [传入了使用妞币数this.currency]
			currencyNum = Integer.parseInt("-" + this.currency);
			pointNum = this.currency;
			break;
		case TYPE_SEND_WEIBO:
			//点击“发送微博信息”
			currencyNum = Integer.parseInt(rule.getCurrency());
			pointNum = Integer.parseInt(rule.getPoint());
			break;
		case TYPE_RANKING_SEND_WEIBO:
			//颁奖一次  [传入了使用妞币数this.currency]
			currencyNum = Integer.parseInt("-" + this.currency);
			pointNum = this.currency;
			break;
		}
		UserInfo userInfo = userInfoService.findByUserName(this.username);

		//处理积分
		UserPointLog userPointLog = new UserPointLog();
		userPointLog.setUsername(this.username);
		userPointLog.setRemark(rule.getContent());
		userPointLog.setPoint(pointNum);
		userPointLog.setRuleid(rule.getId());
		userPointLogService.saveUserPointLog(userPointLog);
		//设置用户的积分信息
		userInfo.setPoint(userInfo.getPoint() + userPointLog.getPoint());

		//处理妞币[妞币信息不为0]
		if(currencyNum != 0) {
			UserCurrencyLog userCurrencyLog = new UserCurrencyLog();
			userCurrencyLog.setCurrency(Float.valueOf(currencyNum));
			userCurrencyLog.setRemark(rule.getContent());
			userCurrencyLog.setRuleid(rule.getId());
			userCurrencyLog.setUsername(this.username);
			userCurrencyLogService.saveUserCurrencyLog(userCurrencyLog);
			//设置用户的妞币信息
			userInfo.setCurrency(userInfo.getCurrency() + userCurrencyLog.getCurrency());
		}

		//处理用户等级信息
		Grade grade = null;
		boolean isUpgrade = false;
		//Grade grade = DataCons.gradeMap.get(userInfo.getGradeid());
		Iterator<Entry<Integer, Grade>> entryKeyIterator = DataCons.gradeMap.entrySet().iterator();
		while (entryKeyIterator.hasNext()) {
			Entry<Integer, Grade> e = entryKeyIterator.next();
			grade = e.getValue();
			String[] points = grade.getUpgradepoint().split("-");
			//用户升级[设置用户等级ID]
			if(userInfo.getGradeid().intValue() != grade.getId().intValue() && userInfo.getPoint().intValue() >= Integer.parseInt(points[0]) && userInfo.getPoint().intValue() <= Integer.parseInt(points[1])) {
				userInfo.setGradeid(grade.getId());
				UserGradeLog userGradeLog = new UserGradeLog();
				userGradeLog.setUsername(this.username);
				userGradeLog.setGradeid(grade.getId());
				userGradeLogService.saveUserGradeLog(userGradeLog);
				isUpgrade = true;
				break;
			}
		}

		//修改用户积分和妞币和等级信息
		userInfoService.updateUserInfoPointCurrency(userInfo);

		//用户升级
		if(isUpgrade) {
			PointCurrencyUtil pointCurrencyUtil = new PointCurrencyUtil(RuleType.TYPE_EACH_UPGRADE, this.username);
			pointCurrencyUtil.handlePointCurrency();

			StationletterService stationletterService = (StationletterService) SpringUtil.getBean("stationletterService");
			Rule r = DataCons.ruleMap.get(RuleType.TYPE_EACH_UPGRADE.getCode());
			String content = String.format(StringCons.STATIONLETTER_UPGRADE_CONTENT, grade.getRemark(), r.getCurrency(), r.getPoint());
			//发送站内信通知
			stationletterService.saveStationletter(StringCons.STATIONLETTER_UPGRADE_TITLE, content, this.username, WeiboCons.system_weiboId);
		}

		//设置获取Session用户信息时同步信息
		BaseController.isSynchroSessionUserinfo = true;
	}
}